package com.zrgk.tag;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.zrgk.util.PageUtil;

public class PageTag extends SimpleTagSupport {
	private PageUtil pageUtil;
	private String key;//获取目标页数的关键字
	private String url;
	
	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}


	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void doTag() throws JspException, IOException {
		PageContext p = (PageContext) this.getJspContext();
		if(pageUtil == null || pageUtil.getTotalCount() == 0)
		{
			p.getOut().write("<span style=\"color:gray;font-size:25;font-weight:700px;\">没有记录</span>");
			return;
		}
		//css
		StringBuilder sb = new StringBuilder();
		sb.append(" <div id=\"yw0\" class=\"pager\">\r\n");
		sb.append("<style type=\"text/css\">");
		sb.append(".pager {text-align: center;}\r\n");
		sb.append(".pager * {text-decoration: none;}\r\n");
		sb.append(".pager .pre-page {display: inline-block;text-align: center;\r\n");
		sb.append("background-color: #ffffff;border-color: #bbbbbb;\r\n");
		sb.append(" border-bottom-left-radius: 20px;border-image-slice: 100%;border-image-source: none;\r\n");
		sb.append(" border-image-width: 1;border-top-left-radius: 20px;\r\n");
		sb.append("border-top-right-radius: 0;box-shadow: rgba(204, 204, 204, 0.701961) 1px 1px 3px 0px;\r\n");
		sb.append("color: #919191;\r\n");
		sb.append("font-family: \"新宋体\", \"宋体\", Arial SimSun, \"Segoe UI\", Tahoma, Helvetica, Sans-Serif;\r\n");
		sb.append("font-size: 14px;font-style: normal;font-variant: normal;\r\n");
		sb.append("font-weight: bold;height: 41px;line-height: 41px;\r\n");
		sb.append(" margin-left: 1px;overflow-x: hidden;overflow-y: hidden;width: 90px;text-decoration: none;}\r\n");
		sb.append(".pager .pre-page:hover {background: #E7F1FE;border: medium none;color: #fff;font-weight: bold;}\r\n");
		sb.append(".pager .next-page {display: inline-block;text-align: center;background-color: #ffffff;border-color: #bbbbbb;\r\n");
		sb.append(" border-bottom-right-radius: 20px;border-top-right-radius: 20px;\r\n");
		sb.append("box-shadow: rgba(204, 204, 204, 0.701961) 1px 1px 3px 0px;color: #919191;\r\n");
		sb.append("font-family: \"新宋体\", \"宋体\", Arial SimSun, 'Segoe UI', Tahoma, Helvetica, Sans-Serif;\r\n");
		sb.append("font-size: 14px; font-style: normal; font-variant: normal;font-weight: bold;\r\n");
		sb.append("height: 41px;line-height: 41px; overflow-x: hidden;\r\n");
		sb.append(" overflow-y: hidden; width: 90px; text-decoration: none;}\r\n");
		sb.append(".pager .next-page:hover {background: #E7F1FE; border: medium none; color: #fff;font-weight: bold;}\r\n");
		sb.append(".pager .item {-webkit-box-shadow: rgba(204, 204, 204, 0.701961) 1px 1px 0px 0px; background-clip: border-box;\r\n");
		sb.append(" background-color: #ffffff;border-color: #919191;\r\n");
		sb.append("box-shadow: rgba(204, 204, 204, 0.701961) 1px 1px 0px 0px;color: #919191;\r\n");
		sb.append("display: inline-block;font-family: Arial, Helvetica, Tahoma, Verdana, Sans-Serif;\r\n");
		sb.append("font-size: 14px; font-weight: bold; height: 41px; line-height: 41px;\r\n");
		sb.append(" overflow: hidden;text-align: center; text-decoration: none;\r\n");
		sb.append(" padding: 0 16px; margin: 0 -1px;}\r\n");
		sb.append(".pager .item:hover {background: #E7F1FE; border: medium none;color: #fff;font-weight: bold;}\r\n");
		sb.append(".pager .cur {background: #E7F1FE; border: medium none;color: #fff;font-weight: bold; cursor: default;}\r\n");
		sb.append(".pager .disabled {color: #bbbbbb;font-weight: normal; cursor: default;}\r\n");
		sb.append(".pager .disabled:hover { background: #FFFFFF;  border: medium none; color: #bbbbbb;font-weight: normal;}\r\n");
		sb.append(".pager .more {  cursor: default;}\r\n");
		sb.append(".pager .more:hover { background: #FFFFFF;  border: medium none;  color: #919191;  font-weight: bold;}\r\n");
		sb.append("</style>\r\n");
		//用表单实现分页
		sb.append("<form method=\"post\" action=\"").append(this.url)  
        .append("\" name=\"qPagerForm\">\r\n");
		//将pageUtil中存的过滤条件取出，表单隐藏域中
		Map<String,String> map = pageUtil.getSearchInfo();
		if(map!=null&&!map.isEmpty())
		{
			Set<Entry<String, String>> entry = map.entrySet();
			for(Entry<String, String> en:entry)
			{
				sb.append("<input type=\"hidden\" name=\"");
				sb.append(en.getKey()).append("\" value=\"");
				sb.append(en.getValue()).append("\" />\r\n");
			}
		}
		int totalPage = pageUtil.getTotalPage();
		int nowPage = pageUtil.getNowPage();
		//将目标页也作为隐藏域
		sb.append("<input type=\"hidden\" name=\"").append(key);
		sb.append("\" value=\"").append(nowPage).append("\" />\r\n");
		 //上一页处理  
        if (nowPage == 1) {  
            sb.append("<a class=\"pre-page disabled\">上一页")  
                .append("</a>\r\n");  
        } else {  
            sb.append("<a class=\"pre-page\" href=\"javascript:turnOverPage(")  
              .append((nowPage - 1))  
              .append(")\">上一页</a>\r\n");  
        }  
          
        //首页，和第二页 ,如果后面页数过多，中间会显示为...
        int start = 1;   
        if(nowPage > 4){  
            start = nowPage - 1;  
            sb.append("<a class=\"item\" href=\"javascript:turnOverPage(1)\">1</a>\r\n");  
            sb.append("<a class=\"item\" href=\"javascript:turnOverPage(2)\">2</a>\r\n");  
            sb.append("<a class=\"item more\" href=\"javascript:void(0);\">...</a>\r\n");  
        }  
        //显示当前页附近的页  
        int end = nowPage + 1;  
        if(end > totalPage){  
            end = totalPage;  
        }  
        for(int i = start; i <= end; i++){  
            if(nowPage == i){   //当前页号不需要超链接  
                sb.append("<a class=\"item cur\">")  
                    .append(i)  
                    .append("</a>\r\n");  
            }else{  
                sb.append("<a class=\"item\" href=\"javascript:turnOverPage(")  
                    .append(i).append(")\">").append(i).append("</a>\r\n");  
            }  
        }  
        //如果后面页数过多,显示"..."  
        if(end < totalPage - 2){  
            sb.append("<a class=\"item more\" href=\"javascript:void(0);\">...</a>\r\n\r\n");  
        }  
        if(end < totalPage - 1){  
            sb.append("<a class=\"item\" href=\"javascript:turnOverPage(")  
            .append(totalPage - 1)  
            .append(")\">")  
            .append(totalPage - 1)  
            .append("</a>\r\n");  
        }  
        if(end < totalPage){  
            sb.append("<a class=\"item\" href=\"javascript:turnOverPage(")  
            .append(totalPage)  
            .append(")\">")  
            .append(totalPage)  
            .append("</a>\r\n");   
        }  
          
        //下一页处理  
        if (nowPage == totalPage) {  
            sb.append("<a class=\"next-page disabled\" href=\"javascript:void(0);\">下一页")  
                .append("</a>\r\n");  
        } else {  
            sb.append("<a class=\"next-page\" href=\"javascript:turnOverPage(")  
                .append((nowPage + 1))  
                .append(")\">下一页</a>\r\n");  
        }  
        sb.append("</form>\r\n");  
  
        // 生成提交表单的JS  
        sb.append("<script language=\"javascript\">\r\n");  
        sb.append("  function turnOverPage(no){\r\n");  
        sb.append("    if(no>").append(totalPage).append("){");  
        sb.append("      no=").append(totalPage).append(";}\r\n");  
        sb.append("    if(no<1){no=1;}\r\n");  
        sb.append("    document.qPagerForm.nowPage.value=no;\r\n");  
        sb.append("    document.qPagerForm.submit();\r\n");  
        sb.append("  }\r\n");  
        sb.append("</script>\r\n");  
        sb.append("</div>");
		p.getOut().write(sb.toString());
	}
	
}
